require(['./config'], () => {
  require(['template','request','footer','header','zoom','fly'], (template ,request,footer) => {
    class Detail {
      constructor () {
      this.init()
      this. addToCart()
      }
      init () {
        // 取id
        const id=window.location.search.slice(4)
        request.getDetail(id).then(resp=>{
          if(resp.code===200){
            const { detail }=resp.data
            //如果用的是rap2，那么返回的detail黎没有正确的id，
           //detail={...resp.data.detail,id}
            const { id, title, price,originPrice, photo, saleNum, descConteneList, commodityTypeName ,couponStataus}=detail
            this.detail={
                   id, title, price,originPrice, photo, saleNum, descConteneList, commodityTypeName ,couponStataus
             }
            $('#design').html(template('src_design',{
              ...this.detail
            }))
            this.zoom()
          }
        })
      }
      zoom(){
        $('.zoom-image').elevateZoom({
          gallery:'gal1'
        })
      }
      addToCart(){
        $('#design').on('click','#add-to-cart-btn',e=>{
          this.fly(e)
          let cartList=localStorage.getItem('cart')
          if(cartList){
            //代表已经存过购物车
             cartList=JSON.parse(cartList)
            //判断已经存过的数据里是否有当前商品
            const isExist=cartList.some(cart=>{
              return cart.id===this.detail.id
            })
            if(isExist){
              //代表当前商品已经存在过
              //如果要修改数组里的元素，用map
              cartList=cartList.map(cart=>{
                if(cart.id===this.detail.id) cart.num++
                return cart
              })
            }else{
              //当前购物车里没有当前数据
              cartList.push({
                id:this.detail.id,
                title:this.detail.title,
                 price:this.detail.price,
                 originPrice:this.detail.originPrice,
                 photo:this.detail.photo[0],
                 saleNum:this.detail.saleNum,
                 num:1,
                 //新加入的商品默认选中
                 check:true
              })
            }
            localStorage.setItem('cart',JSON.stringify(cartList))
            
          }else{
           
            localStorage.setItem('cart',JSON.stringify([{
              id:this.detail.id,
              title:this.detail.title,
               price:this.detail.price,
               originPrice:this.detail.originPrice,
               photo:this.detail.photo[0],
               saleNum:this.detail.saleNum,
              num:1,
              check:true
            }]))
          }
        })
      }
      fly(e){
        $(`<img src="${this.detail.photo[0].url}" style="width:30px; height:30px;">`).fly({
          start:{
            left:e.clientX,
            top:e.clientY,
          },
          end:{
            left:$(window).width(),
            top:600,
            width:90,
            height:0,
          },
      
          speed:0.8,
          vertex_Rtop:10,
          onEnd:function(){
            this.destroy()
            //购物车数量修改
            footer.calcCartNum()
          }
        });
      }
     }
     new Detail()
  })
})